package com.healer.springcloud.controller;

import com.healer.springcloud.pojo.Department;
import com.healer.springcloud.service.Impl.DepartmentServiceImpl;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//提供restful服务
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    DepartmentServiceImpl departmentService;
    @HystrixCommand(fallbackMethod = "hystrixQueryDepartmentById")
    @PostMapping("/querydepartmentbyid")
    public Department queryDepartmentById(@RequestBody Map<String,Object> map){
        System.out.println("我来到了spring-cloud-provider-department-hystrix-8001-queryDepartmentById");
        if(map==null){
            throw new RuntimeException("没有数据传入");
        }
        Department id = departmentService.queryDepartmentById(Long.parseLong(map.get("id").toString()));
        if(id==null){
            throw new RuntimeException(map.get("id").toString()+"为空");
        }
        return id;
    }
//备选方法
    public Department hystrixQueryDepartmentById(@RequestBody Map<String,Object> map){
        return new Department(Long.parseLong(map.get("id").toString()),
                   "这个用户不存在","没有数据库");
    }
}
